﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Modules.Logs;
using Modules.SMS.Entitys;
using PetaPoco;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.Caching;
using PmSoft.DBContext;
using PmSoft.Repositories;
using PmSoft.Utilities;

namespace Modules.SMS.Repositories
{
    [UseDI(ServiceLifetime.Scoped)]
    public class SmsSendLogRepository : Repository<SmsSendLog, DefaultDbContext>
    {
        private readonly ILogger logger;

        public SmsSendLogRepository(DefaultDbContext dbContext, IApplicationContext applicationContext, ILogger<SmsSendLogRepository> logger)
            : base(dbContext, applicationContext)
        {
            this.logger = logger;
        }


        public IEnumerable<SmsSendLog> GetSendLogs(string mobile, SMSSendQuery query)
        {
            Sql sql = Sql.Builder;
            sql.Select("msg_id")
            .From("sms_send_log")
            .Where("phone=@0", mobile);
            if (query.Type.HasValue)
                sql.Where("msg_type=@0", query.Type.Value);
            if (query.EndDate.HasValue)
                sql.Where("create_time > @0", query.EndDate.Value);
            sql.OrderBy("create_time DESC")
                .Append("LIMIT 100");
            var ids = DbContext.FetchFirstColumn<string>(sql);
            return PopulateEntitiesByEntityIds(ids);
        }

        /// <summary>
        /// 合计发送数量
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public int TotalSendCount(string mobile, SMSSendQuery query)
        {
            Sql sql = Sql.Builder;
            sql.Select("COUNT(0)")
            .From("sms_send_log")
            .Where("phone=@0", mobile);
            if (query.Type.HasValue)
                sql.Where("msg_type=@0", query.Type.Value);
            if (query.EndDate.HasValue)
                sql.Where("create_time > @0", query.EndDate.Value);

            return DbContext.ExecuteScalar<int>(sql);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="query"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IPagedList<SmsSendLog> GetSendLogs(SmsPageQuery query, int pageIndex, int pageSize)
        {
            Sql sql = Sql.Builder;
            sql.Select("msg_id")
                .From("sms_send_log");
            //这里根据自己的查询参数判断 
            if (!string.IsNullOrEmpty(query.Keywords))
                sql.Where("(msg_content Like @0 or phone like @0 or result_msg like @0) ", "%" + StringUtility.StripSQLInjection(query.Keywords) + "%");
            if (query.StartTime.HasValue)
                sql.Where("create_time>=@0", query.StartTime.Value);
            if (query.EndTime.HasValue)
                sql.Where("create_time<=@0", query.EndTime.Value);
            if (!string.IsNullOrEmpty(query.Phone))
                sql.Where("phone like @0", "%" + StringUtility.StripSQLInjection(query.Phone) + "%");
            if(query.MsgType.HasValue)
                sql.Where("msg_type=@0", query.MsgType.Value);
            sql.OrderBy("create_time DESC");
            return GetPagingEntities(pageSize, pageIndex, sql);
        }
    }
}